package mate.dart.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import mate.dart.AbstractGame;
import mate.dart.ThrowEvent;
import mate.dart.utils.Utils;

public class GameCricetScoreBoard extends AbstractGame {

	private static final long serialVersionUID = 1L;
	
	int game = 301;
	
	int round = 1;
	int currentPlayer = 0;
	private int players = 2;
	int throwsNumber = 0;

	String beforeRound = "";
	
	PlayerCounter[] playersCounters = new PlayerCounter[players];
	JTextField roundField = new JTextField("1");
	JLabel leftDarts = new JLabel("Left Darts: 3");
	JTextArea info = new JTextArea("");

	public GameCricetScoreBoard(int game) {
		initComponents();
	}
	
	public GameCricetScoreBoard() {
		this(301);
	}
	
	public String getName() {
		return "Cricet";
	}
	
	
	
	@Override
	public void repaint() {
		super.repaint();
		if (playersCounters != null) colorCounters();
	}

	private void initComponents() {
		playersCounters = new PlayerCounter[players];
		for (int i = 0; i < players; i++) {
			playersCounters[i] = new PlayerCounter();
		}
		
		setLayout(new BorderLayout());
		Box vBox = Box.createVerticalBox();
		vBox.add(Box.createVerticalStrut(20));
		JLabel label;
		label = new JLabel("You are playing the Cricet");
		label.setFont(Utils.getSmallFont());
		vBox.add(label);
		vBox.add(Box.createVerticalStrut(20));
		
		Box box = Box.createHorizontalBox();
		label = new JLabel("Round: ");
		label.setFont(Utils.getSmallFont());
		box.add(label);
		box.add(Box.createHorizontalStrut(10));
		roundField.setMaximumSize(new Dimension(25, 25));
		roundField.setFont(Utils.getSmallFont());
		roundField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				round = Integer.parseInt(roundField.getText());
			}
		});
		box.add(roundField);
		vBox.add(box);
		
		box = Box.createHorizontalBox();
		for (int i = 0; i < players; i++) {
			box.add(playersCounters[i]);			
		}
		vBox.add(box);
		
		box = Box.createVerticalBox();
		leftDarts.setFont(Utils.getSmallFont());
		info.setBackground(new JPanel().getBackground());
		info.setFont(Utils.getSmallFont());
		info.setEditable(false);
		box.add(leftDarts);
		box.add(info);
		vBox.add(box);
		
		add(vBox);
		
		colorCounters();
	}
	
	public void newThrowEvent(ThrowEvent event) {
		throwsNumber++;
		leftDarts.setText("Left Darts: " + (3 - throwsNumber));
		int num = event.getNum();
			
		if (event.isAllDarts()) {
			throwsNumber = 3;
			num= 0;
		}

		if (num >= 15) {
			PlayerCounter current = playersCounters[currentPlayer];
			int count = event.getCount();
			int pcount = current.getNumCount(num);
			int toAdd = 3 - pcount;
			if (toAdd >= count ) {
				current.addNumCount(num, count);
			} else {
				current.addNumCount(num, toAdd);
				for (PlayerCounter p : playersCounters)
					if (p != current && p.getNumCount(num) != 3) {
						p.addPoints((count - toAdd)*num);
					}
			}
		}

		if (throwsNumber == 3) {
			if (currentPlayer == players - 1) incRound();
			changePlayer();
		}

	}
	
	public void clear() {
		throwsNumber = 0;
		currentPlayer = 0;
		round = 0;
		incRound();
		for (PlayerCounter p : playersCounters) {
			p.clear();
		}
		leftDarts.setText("Left Darts: 3");
		info.setText("");
		colorCounters();
	}
	
	private void changePlayer() {
		currentPlayer = (currentPlayer + 1 ) % players;
		throwsNumber = 0;
		leftDarts.setText("Left Darts: 3");
		info.setText(getInfoString());
		colorCounters();
	}
	
	private String getInfoString() {
		return "";
	}
	
	private void incRound() {
		round++;
		roundField.setText(round + "");
	}
	
	public void setPlayers(int players) {
		removeAll();
		this.players = players;
		initComponents();
		leftDarts.setText(""); //to force repainting
		clear();
	}

	private void colorCounters() {
		for (PlayerCounter p : playersCounters) {
			p.lightLabels(playersCounters[currentPlayer] == p);
		}
	}
	
	private static class PlayerCounter extends JPanel {
		
		private static final long serialVersionUID = 1L;
		JTextField pointsField = new JTextField("0");
		JTextField playerName = new JTextField("name");
		JTextField[] labels = new JTextField[7];
		
		public PlayerCounter() {
			initComponents();
			setPreferredSize(new Dimension(200, 300));
		}
		
		private void initComponents() {
			setLayout(new GridLayout(9, 1));
			
			playerName.setFont(Utils.getSmallFont());
			add(playerName);			
			
			pointsField.setFont(Utils.getNormalFont());
			add(pointsField);
			
			for (int i = 0; i < 6; i++) {
				JTextField l = new JTextField((20 - i) + " ");
				l.setFont(Utils.getNormalFont());
				labels[i] = l;
				l.setOpaque(true);
				l.setBorder(BorderFactory.createEmptyBorder());
				l.setBackground(getBackground());
				add(l);
			}
			labels[6] = new JTextField("25 ");
			labels[6].setBackground(getBackground());
			labels[6].setBorder(BorderFactory.createEmptyBorder());
			labels[6].setOpaque(true);
			labels[6].setFont(Utils.getNormalFont());
			add(labels[6]);
		}
		
		public void lightLabels(boolean light) {
			for (JTextField l : labels) {
				if (light) {
					l.setBackground(Color.green);
				} else {
					l.setBackground(getBackground());
				}
			}
		}
		
		public int getNumCount(int num){
			int index = getIndex(num);
			int i = labels[index].getText().lastIndexOf("x");
			if ( i == -1) {
				return 0;
			} else {
				return i - 2;
			}
		}
		
		public void addNumCount(int num, int count) {
		
			StringBuilder s = new StringBuilder();
			for (int i = 0; i < count; i++) {
				s.append("x");
			}
 
			int index = getIndex(num);
			labels[index].setText(labels[index].getText() + s.toString());
		}
		
		public void addPoints(int points) {
			pointsField.setText((Integer.parseInt(pointsField.getText()) + points) + "");
		}
		
		public void clear() {
			pointsField.setText("0");
			for (int i = 0; i < 6; i++) {
				labels[i].setText((20 - i) + " ");
			}
			labels[6].setText("25 ");
			lightLabels(false);
		}
		
		private int getIndex(int num) {
			if (num == 25) {
				return 6;
			} else {
				return 20 -num;
			}
		}
	}
}
